import 'package:dynamic_color/dynamic_color.dart';
import 'package:flutter/material.dart';
import 'package:flutter_sercry_manage/page/home/home_page.dart';
import 'package:flutter_sercry_manage/provider/provider.dart';

import 'color_schemes.g.dart';
import 'custom_color.g.dart';

void main() {
  runApp(const MyApp());
}

class ThemeModeNotifier extends ChangeNotifier {
  ThemeMode _mode = ThemeMode.light;

  set mode(ThemeMode mode) {
    _mode = mode;
    notifyListeners();
  }

  ThemeMode get mode => _mode;
}

class MyApp extends StatefulWidget {
  const MyApp({super.key});

  @override
  State<StatefulWidget> createState() => MyAppState();
}

class MyAppState extends State<MyApp> {

  @override
  void initState() {
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return DynamicColorBuilder(builder: (lightDynamic, darkDynamic) {
      ColorScheme lightScheme;
      ColorScheme darkScheme;

      if (lightDynamic != null && darkDynamic != null) {
        lightScheme = lightDynamic.harmonized();
        lightCustomColors = lightCustomColors.harmonized(lightScheme);

        // Repeat for the dark color scheme.
        darkScheme = darkDynamic.harmonized();
        darkCustomColors = darkCustomColors.harmonized(darkScheme);
      } else {
        // Otherwise, use fallback schemes.
        lightScheme = lightColorScheme;
        darkScheme = darkColorScheme;
      }

      return Provider<ThemeModeNotifier>(
          changeNotifier: ThemeModeNotifier(),
          builder: (context) => MaterialApp(
            title: '绝对安全的密码，理论的取值可能范围最高达：330,225,942,528,000',
            theme: ThemeData(
                useMaterial3: true,
                colorScheme: lightColorScheme,
                extensions: [lightCustomColors]),
            darkTheme: ThemeData(
                useMaterial3: true,
                colorScheme: darkColorScheme,
                extensions: [darkCustomColors]),
            //【理论的取值可能范围最高达：330,225,942,528,000】
            home: const MyHomePage(title: '绝对安全的密码管理器'),
            themeMode: context.read<ThemeModeNotifier>().mode,
          ));
    });
  }
}
